Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yy-loop

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yy-loop

Simple and configurable game/main loop.

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

yy-loop

Simple and configurable game/main loop

features

  • emits events (using eventemitter3)
  • run every frame or after an interval
  • control how many times each function runs
  • loop pauses when app loses focus
  • uses requestAnimationFrame()

rationale

This is a replacement for YY-Update. I use this in all my games, and as a base class for my other libraries, including pixi-ease and pixi-viewport.

installation

npm i yy-loop

simple example

const Loop = require('yy-loop')

const loop = new Loop()

// add a function to the loop that logs the elapsed time
loop.add((elapsed) => console.log(elapsed))

// add a function to the loop that runs ever second for 5 times
const entry = loop.add((elapsed) => console.log(elapsed), 1000, 5)

// run each time entry is called
entry.on('each', () => console.log('It has been one second.'))

// run after entry is complete
entry.on('done', () => console.log('completed'))

live example

https://davidfig.github.io/loop/

API

src/loop.js

    /**
     * basic loop support
     * note: the default is to stop the loop when app loses focus
     * @param {object} [options]
     * @param {number} [options.maxFrameTime=1000 / 60] maximum time in milliseconds for a frame
     * @param {object} [options.pauseOnBlur] pause loop when app loses focus, start it when app regains focus
     *
     * @event each(elapsed, Loop)
     * @event start(Loop)
     * @event stop(Loop)
     */
    constructor(options)

    /**
     * start requestAnimationFrame() loop
     * @return {Loop} this
     */
    start()

    /**
     * stop loop
     * @return {Loop} this
     */
    stop()

    /**
     * loop through updates
     */
    update()

    /**
     * add a callback to the update loop
     * @param {function} callback
     * @param {number} [time=0] in milliseconds to call this update
     * @param {number} [count=0] number of times to run this update (0=infinite)
     * @return {object} entry - used to remove or change the parameters of the update
     */
    add(callback, time, count)

    /**
     * remove a callback from the loop
     * @param {object} entry - returned by add()
     */
    remove(entry)

    /**
     * removes all callbacks from the loop
     */
    removeAll()

    /**
     * @type {number} count of all animations
     */
    get count()

    /**
     * @type {number} count of running animations
     */
    get countRunning()

src/entry.js

/** Entry class for Loop */
class Entry extends Events
{
    /**
     * create an entry in the update loop
     * used by Loop
     * @param {function} callback
     * @param {number} [time=0] in milliseconds to call this update
     * @param {number} [count] number of times to run this update (undefined=infinite)
     */
    constructor(callback, time, count)

    /**
     * update checks time and runs the callback
     * @param {number} elapsed
     * @return {boolean} whether entry is complete and may be removed from list
     */
    update(elapsed)

    /**
     * @type {boolean} pause this entry
     */
    set pause(value)

License

MIT License
(c) 2017 YOPEY YOPEY LLC by David Figatner

Keywords

FAQs

Package last updated on 07 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc